//
//  MessageListNormalViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/9/2.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MessageListNormalViewController.h"

@interface MessageListNormalViewController ()

@end

@implementation MessageListNormalViewController
- (instancetype)initWithViewModel:(BaseViewModel<MessageListNormalViewModelDelegate> *)viewModel
{
    self = [super initWithTableRefreshType:TYPE_TABLE_REFRESH_PULL_UP_DOWN];
    if (self) {
        self.viewModel = viewModel;
        self.viewModel.viewController = self;

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = ([self.viewModel messageType] == MessageCenter_Service ?  @"服务消息" : ([self.viewModel messageType] == MessageCenter_Insurance ? @"保险消息" :([self.viewModel messageType] == MessageCenter_System ? @"系统消息" : @"")));
    
    [self createTableView];
    
    [self.view addSubview:[self.viewModel returnNoDataView]];
    [self.viewModel reloadNoDataHidden:[self.viewModel MessageNums] > 0];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.viewModel viewDidDisappear];
}
- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, 0 , __MAIN_WIDTH , __MAIN_HEIGHT_64);
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [Theme lineNewColor];
}
#pragma mark -

-(void)loadMoreTopFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    [self.viewModel updateSuccess:^(NSInteger num) {
        finish(0);
    } fail:^{
        finish(0);
    }];
}
-(void)loadMoreBottomFinish:(void (^)())finish withScrollView:(UIScrollView *)scrollView
{
    [self.viewModel loadMoreSuccess:^(NSInteger num) {
        finish();
    } fail:^{
        finish();
    }];
}

#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel MessageNums];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return [MessageListNormalCell getCellHeightWithModel:[self.viewModel messageAtIndex:indexPath.row] type:[self.viewModel messageType]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellID = @"MessageCenterCell";
    MessageListNormalCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MessageListNormalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    __weak __typeof(self) wself = self;
    cell.clickLabelURL = ^(NSString *url)
    {
        [wself.viewModel clickLabelTextUrl:url];
    };
    
    [cell reloadDataWithModel:[self.viewModel messageAtIndex:indexPath.row] type:[self.viewModel messageType]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([Check isVisiter])
    {
        return;
    }
    [self.viewModel clickOnMessageAtIndex:indexPath.row];
    
    //    [UmengMgr event:ICZ_CarMgrEditCar];
    //    [[StatisticsMgr sharedInstance] addClickID:@"我的车辆－编辑车辆" description:[self.tableView cellForRowAtIndexPath:indexPath].description];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
